package com.csot.ry.controller;

import com.csot.common.annotation.Log;
import com.csot.common.constant.HttpStatus;
import com.csot.common.core.controller.BaseController;
import com.csot.common.core.domain.AjaxResult;
import com.csot.common.core.page.TableDataInfo;
import com.csot.common.enums.BusinessType;
import com.csot.common.utils.PageUtils;
import com.csot.common.utils.poi.ExcelUtil;
import com.csot.model.CitySegment;
import com.csot.ry.domain.TIdleRecord;
import com.csot.ry.service.ITIdleRecordService;
import com.csot.ry.util.CacheKitUtil;
import com.csot.ry.util.PageUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Objects;

/**
 * 备案稽核Controller
 *
 * @author csot
 * @date 2023-04-10
 */
@RestController
@RequestMapping("/ipmanagement/idlerecord")
public class TIdleRecordController extends BaseController {
    @Autowired
    private ITIdleRecordService tIdleRecordService;
    final CitySegment citySegment = new CitySegment().dao();

    /**
     * 查询备案稽核列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TIdleRecord tIdleRecord, HttpServletRequest request) {
        List<TIdleRecord> list = tIdleRecordService.selectTIdleRecordList(tIdleRecord);
        HttpSession hs = request.getSession();
        hs.setAttribute("idlerecord", list);
        startPage();
        Page page = PageUtils.getLocalPage();
        PageInfo<TIdleRecord> pi = PageUtil.startPage(list, page.getPageNum(), page.getPageSize());
        List<TIdleRecord> plist = pi.getList();
        plist.forEach(f -> {
            CitySegment cs = citySegment.findFirst("select * from t_city_segment where ip_start_i <= ? and ip_end_i >= ?",
                    f.getIpStartI(), f.getIpStartI());
            if (Objects.nonNull(cs)) {
                f.setIpCity(CacheKitUtil.ip_city.get(cs.getIpCity()));
                f.setBusinessNumber(cs.getBusinessNumber());
                f.setIpApplication(cs.getIpApplication());
                f.setIpUser(cs.getIpUser());
                f.setIpUseType(CacheKitUtil.ip_use_type.get(cs.getIpUseType()));
                f.setIpEquipment(cs.getIpEquipment());
                f.setIpManagement(cs.getIpManagement());
                f.setIpUsername(cs.getIpUsername());
                f.setIpSubnet(cs.getIpUsername());
                f.setIpStatus(CacheKitUtil.ip_status.get(cs.getIpStatus()));
            } else {
                System.out.println("---------------" + f.getIpStartI());
            }
        });
        return getDataTable(plist, list.size());
    }

    /**
     * 导出备案稽核列表
     */
    @Log(title = "备案稽核", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TIdleRecord tIdleRecord, HttpServletRequest request) {
        List<TIdleRecord> list = null;
        HttpSession hs = request.getSession();
        if (Objects.nonNull(hs.getAttribute("idlerecord"))) {
            list = (List<TIdleRecord>) hs.getAttribute("idlerecord");
        } else {
            list = tIdleRecordService.selectTIdleRecordList(tIdleRecord);
        }
        list.parallelStream().forEach(f -> {
            CitySegment cs = citySegment.findFirst("select * from t_city_segment where ip_start_i <= ? and ip_end_i >= ?",
                    f.getIpStartI(), f.getIpStartI());
            if (Objects.nonNull(cs)) {
                f.setIpCity(CacheKitUtil.ip_city.get(cs.getIpCity()));
                f.setBusinessNumber(cs.getBusinessNumber());
                f.setIpApplication(cs.getIpApplication());
                f.setIpUser(cs.getIpUser());
                f.setIpUseType(CacheKitUtil.ip_use_type.get(cs.getIpUseType()));
                f.setIpEquipment(cs.getIpEquipment());
                f.setIpManagement(cs.getIpManagement());
                f.setIpUsername(cs.getIpUsername());
                f.setIpSubnet(cs.getIpUsername());
                f.setIpStatus(CacheKitUtil.ip_status.get(cs.getIpStatus()));
            } else {
                System.out.println("---------------" + f.getIpStartI());
            }
        });

        ExcelUtil<TIdleRecord> util = new ExcelUtil<TIdleRecord>(TIdleRecord.class);
        util.exportExcel(response, list, "备案稽核数据");
    }

    /**
     * 获取备案稽核详细信息
     */
    @PreAuthorize("@ss.hasPermi('ipmanagement:idlerecord:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tIdleRecordService.selectTIdleRecordById(id));
    }

    /**
     * 新增备案稽核
     */
    @PreAuthorize("@ss.hasPermi('ipmanagement:idlerecord:add')")
    @Log(title = "备案稽核", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TIdleRecord tIdleRecord) {
        return toAjax(tIdleRecordService.insertTIdleRecord(tIdleRecord));
    }

    /**
     * 修改备案稽核
     */
    @PreAuthorize("@ss.hasPermi('ipmanagement:idlerecord:edit')")
    @Log(title = "备案稽核", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TIdleRecord tIdleRecord) {
        return toAjax(tIdleRecordService.updateTIdleRecord(tIdleRecord));
    }

    /**
     * 删除备案稽核
     */
    @PreAuthorize("@ss.hasPermi('ipmanagement:idlerecord:remove')")
    @Log(title = "备案稽核", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tIdleRecordService.deleteTIdleRecordByIds(ids));
    }

    protected TableDataInfo getDataTable(List<?> list, long total) {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(list);
        rspData.setTotal(total);
        return rspData;
    }
}
